package com.simple.datax.web;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.simple.datax.bo.IDataxDictionaryBO;
import com.simple.datax.bo.IDataxFlowActivityTemplateBO;
import com.simple.datax.bo.IDataxFlowActorsTemplateBO;
import com.simple.datax.bo.IDataxFlowTemplateBO;
import com.simple.datax.vo.DataxFlowActivityTemplateIdVO;
import com.simple.datax.vo.DataxFlowActivityTemplateVO;
import com.simple.datax.vo.DataxFlowActorsTemplateIdVO;
import com.simple.datax.vo.DataxFlowActorsTemplateVO;
import com.simple.datax.vo.DataxFlowTemplateVO;
import com.simple.util.common.Constants;
import com.simple.util.common.ErrorUtil;
import com.simple.util.common.StringUtil;
import com.simple.util.exception.SimpleApplicationException;
import com.simple.util.object.JFValidationBean;
import com.simple.util.object.PageInfoBean;

@Controller 
public class BusinessFlowController extends AbstractDataxBaseController{
	private final static String JSP_PATH = "_BusinessFlow";
	
	/** The LOG. */
	private static Logger LOG = Logger.getLogger(BusinessFlowController.class);

	@Autowired
	private IDataxFlowTemplateBO dataxFlowTemplateBO;
	
	@Autowired
	private IDataxFlowActorsTemplateBO dataxFlowActorsTemplateBO;
	
	@Autowired
	private IDataxFlowActivityTemplateBO dataxFlowActivityTemplateBO;
	
	@Autowired
	private IDataxDictionaryBO dataxDictionaryBO;
	
	private static String JSP_PATH_BUSINESS_FLOW_TEMPLATE = "business_flow_template";
	
	private static String JSP_PATH_BUSINESS_FLOW_TEMPLATE_ADD = "business_flow_template_add";
	
	private static String JSP_PATH_BUSINESS_FLOW_ACTORS_TEMPLATE = "business_flow_actors_template";
	
	private static String JSP_PATH_BUSINESS_FLOW_ACTORS_TEMPLATE_ADD = "business_flow_actors_template_add";
	
	private static String JSP_PATH_BUSINESS_FLOW_ACTIVITY_TEMPLATE = "business_flow_activity_template";
	
	private static String JSP_PATH_BUSINESS_FLOW_ACTIVITY_TEMPLATE_ADD = "business_flow_activity_template_add";
	
	private static String JSP_PATH_BUSINESS_FLOW_TEMPLATE_VIEW = "business_flow_template_view";
	
	private static String IS_EDIT = "isEdit";
	
	private static String[] ATTR_NAME = {"flowTemplateName","flowTemplateId","businessType","creatCorp","status","notes","dataxFlowTemplateVO","activityOrder",
		"dataxFlowActivityTemplateVOList","dataxFlowActorsTemplateVOList","actorNo","activityNo"};

	@RequestMapping("/businessFlow")  
    public String init(@RequestParam(value="pageNo",required=false,defaultValue="1") Integer pageNo,
			   @RequestParam(value="flowTemplateNameSelect",required=false) String flowTemplateNameSelect,
			   @RequestParam(value="flowTemplateIdSelect",required=false) String flowTemplateIdSelect,
			   @RequestParam(value="businessTypeSelect",required=false) String businessTypeSelect,
			   @RequestParam(value="creatTimeStart",required=false) String creatTimeStart,
			   @RequestParam(value="creatTimeEnd",required=false) String creatTimeEnd,
			   @RequestParam(value="createrSelect",required=false) String createrSelect,
			   @RequestParam(value="statusSelect",required=false) String statusSelect,
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.init");
		this.removeSessionAttribute(request);
        
        Map<String, String> optionTemplateStatusMap = Constants.TEMPLATE_STATE_MAP;
        getHttpSession(request).setAttribute("optionTemplateStatusMap", optionTemplateStatusMap);
		Map<String, String> optionOperationTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_OPERATIONTYPE);
		getHttpSession(request).setAttribute("optionOperationTypeMap", optionOperationTypeMap);       
        Map<String, String> optionActorTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_ACTORTYPE);
        getHttpSession(request).setAttribute("optionActorTypeMap", optionActorTypeMap);
        Map<String, String> optionBusinessRoleMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_BUSINESSROLE);
        getHttpSession(request).setAttribute("optionBusinessRoleMap", optionBusinessRoleMap);
        

        //查询列表
		final PageInfoBean pageInfoBean = new PageInfoBean(pageNo);
        final List<DataxFlowTemplateVO> dataxFlowTemplateVOList = dataxFlowTemplateBO.findListByPage(flowTemplateNameSelect, flowTemplateIdSelect, 
        		businessTypeSelect, creatTimeStart, creatTimeEnd, createrSelect, statusSelect, pageInfoBean);
        request.setAttribute("dataxFlowTemplateVOList", dataxFlowTemplateVOList);
        
		final String pageInfo = super.generatePageInfo(request,response,pageInfoBean, "goPage");
	    request.setAttribute("pageInfo", pageInfo);  
        request.setAttribute("pageNo", pageNo);
        
        request.setAttribute("flowTemplateNameSelect", flowTemplateNameSelect);
        request.setAttribute("flowTemplateIdSelect", flowTemplateIdSelect);
        request.setAttribute("businessTypeSelect", businessTypeSelect);
        request.setAttribute("creatTimeStart", creatTimeStart);
        request.setAttribute("creatTimeEnd", creatTimeEnd);
        request.setAttribute("createrSelect", createrSelect);
        request.setAttribute("statusSelect", statusSelect);
        return getJspPath(JSP_PATH_BUSINESS_FLOW_TEMPLATE);
    }
	
	@RequestMapping("/addFlowTemplate")  
    public String addFlowTemplate(final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addFlowTemplate");
		request.getSession().removeAttribute(IS_EDIT);
	    String flowTemplateId = null;
	    flowTemplateId = Constants.PREFIX_TEMPLATE_ID + StringUtil.leftPad(dataxFlowTemplateBO.generateId(), "0", 6);
	    LOG.debug("flowTemplateId="+flowTemplateId);
	    getHttpSession(request).setAttribute("flowTemplateId", flowTemplateId);
	   
		return getJspPath(JSP_PATH_BUSINESS_FLOW_TEMPLATE_ADD);
	}
	
	@RequestMapping("/addFlowTemplateNext")  
    public String addFlowTemplateNext(@RequestParam(value="flowTemplateName",required=false) String flowTemplateName,
			   @RequestParam(value="flowTemplateId",required=false) String flowTemplateId,
			   @RequestParam(value="businessType",required=false) String businessType,
			   @RequestParam(value="creatCorp",required=false) String creatCorp,
			   @RequestParam(value="status",required=false) String status,
			   @RequestParam(value="notes",required=false) String notes,
			   @ModelAttribute("DataxFlowTemplateVO") DataxFlowTemplateVO dataxFlowTemplateVO,
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addFlowTemplateNext");
//		getHttpSession(request).setAttribute("flowTemplateName", flowTemplateName);
//		getHttpSession(request).setAttribute("flowTemplateId", flowTemplateId);
//		getHttpSession(request).setAttribute("businessType", businessType);
//		getHttpSession(request).setAttribute("creatCorp", creatCorp);
//		getHttpSession(request).setAttribute("status", status);
//		getHttpSession(request).setAttribute("notes", notes);
		getHttpSession(request).setAttribute("dataxFlowTemplateVO", dataxFlowTemplateVO);
		
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTORS_TEMPLATE);
	}
	
	@RequestMapping("/addFlowActorsTemplate")  
    public String addFlowActorsTemplate(
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addFlowActorsTemplate");
        
        String actorNo = null;
        
        actorNo = (String)getHttpSession(request).getAttribute("actorNo");
        if (StringUtils.isBlank(actorNo)){
        	actorNo = Constants.PREFIX_NODE_ID + StringUtil.leftPad("1", "0", 2);
        }else{
        	actorNo = Constants.PREFIX_NODE_ID + StringUtil.leftPad(String.valueOf(Integer.parseInt(actorNo.substring(1))+1),"0",2);
        }
        LOG.debug("actorNo="+actorNo);
        getHttpSession(request).setAttribute("actorNo", actorNo);
                
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTORS_TEMPLATE_ADD);
	}
	
	@SuppressWarnings(value="unchecked")
	@RequestMapping("/addFlowActorsTemplateSave")  
    public String addFlowActorsTemplateSave(@RequestParam(value="actorNo",required=false) String actorNo,
			   @RequestParam(value="actorName",required=false) String actorName,
			   @RequestParam(value="actorAlias",required=false) String actorAlias,
			   @RequestParam(value="actorType",required=false) String actorType,
			   @RequestParam(value="businessRole",required=false) String businessRole,
			   @RequestParam(value="notes",required=false) String notes,
			   @ModelAttribute("DataxFlowActorsTemplateVO") DataxFlowActorsTemplateVO dataxFlowActorsTemplateVO,
			   @ModelAttribute("DataxFlowActorsTemplateIdVO") DataxFlowActorsTemplateIdVO dataxFlowActorsTemplateIdVO,
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addFlowActorsTemplateSave");
		
		List<DataxFlowActorsTemplateVO> dataxFlowActorsTemplateVOList = 
				(List<DataxFlowActorsTemplateVO>)getHttpSession(request).getAttribute("dataxFlowActorsTemplateVOList");
		if(null == dataxFlowActorsTemplateVOList){
			dataxFlowActorsTemplateVOList = new ArrayList<DataxFlowActorsTemplateVO>();
		}
		dataxFlowActorsTemplateVO.setId(dataxFlowActorsTemplateIdVO);
		dataxFlowActorsTemplateVOList.add(dataxFlowActorsTemplateVO);
		getHttpSession(request).setAttribute("dataxFlowActorsTemplateVOList", dataxFlowActorsTemplateVOList);
		
		getHttpSession(request).removeAttribute("actorName");
		getHttpSession(request).removeAttribute("actorAlias");
		getHttpSession(request).removeAttribute("actorType");
		getHttpSession(request).removeAttribute("businessRole");
		getHttpSession(request).removeAttribute("notes");
		
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTORS_TEMPLATE);
	}
	
	@SuppressWarnings(value="unchecked")
	@RequestMapping("/removeFlowActorsTemplate")  
    public String removeFlowActorsTemplate(@RequestParam(value="actorNo",required=false) String actorNo,
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.removeFlowActorsTemplate");
		
		List<DataxFlowActorsTemplateVO> dataxFlowActorsTemplateVOList = 
				(List<DataxFlowActorsTemplateVO>)getHttpSession(request).getAttribute("dataxFlowActorsTemplateVOList");
		for(DataxFlowActorsTemplateVO dataxFlowActorsTemplateVO:dataxFlowActorsTemplateVOList){
			if(null != actorNo && actorNo.equals(dataxFlowActorsTemplateVO.getId().getActorNo())){
				boolean flag = true;
				List<DataxFlowActivityTemplateVO> dataxFlowActivityTemplateVOList = 
						(List<DataxFlowActivityTemplateVO>)getHttpSession(request).getAttribute("dataxFlowActivityTemplateVOList");
				if(null != dataxFlowActivityTemplateVOList){
					for(DataxFlowActivityTemplateVO dataxFlowActivityTemplateVO:dataxFlowActivityTemplateVOList){
						String actorNoEnd = dataxFlowActivityTemplateVO.getActorEnd();
						String actorNoStart = dataxFlowActivityTemplateVO.getActorStart();
						//删除node之前应该删除相关activity
						if (actorNoEnd!=null && actorNo.equals(actorNoEnd)){
							final List<JFValidationBean> errorList = getErrorList();
							errorList.add(ErrorUtil.generateJFValidationFromAppException(new SimpleApplicationException("message.businessflow.del")));
							flag = false;
							break;
						}
						if (actorNoStart!=null && actorNo.equals(actorNoStart)){
							final List<JFValidationBean> errorList = getErrorList();
							errorList.add(ErrorUtil.generateJFValidationFromAppException(new SimpleApplicationException("message.businessflow.del")));
							flag = false;
							break;
						}
					}
				}
				if (flag == true){
					dataxFlowActorsTemplateVOList.remove(dataxFlowActorsTemplateVO);
				}
				break;
			}
		}
		getHttpSession(request).setAttribute("dataxFlowActorsTemplateVOList", dataxFlowActorsTemplateVOList);
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTORS_TEMPLATE);
	}
	
	@SuppressWarnings(value="unchecked")
	@RequestMapping("/addFlowActorsTemplateNext")  
    public String addFlowActorsTemplateNext(final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addFlowActorsTemplateNext");
		List<DataxFlowActorsTemplateVO> dataxFlowActorsTemplateVOList = (List<DataxFlowActorsTemplateVO>)getHttpSession(request).getAttribute("dataxFlowActorsTemplateVOList");
		if (null==dataxFlowActorsTemplateVOList || dataxFlowActorsTemplateVOList.size()<2){
			final List<JFValidationBean> errorList = getErrorList();
			errorList.add(ErrorUtil.generateJFValidationFromAppException(new SimpleApplicationException("message.businessflow.ActorsSubmit")));
			return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTORS_TEMPLATE);
		}
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTIVITY_TEMPLATE);
	}
	
	@RequestMapping("/addFlowActorsTemplatePrev")  
    public String addFlowActorsTemplatePrev(final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addFlowActorsTemplatePrev");
		DataxFlowTemplateVO dataxFlowTemplateVO =(DataxFlowTemplateVO)getHttpSession(request).getAttribute("dataxFlowTemplateVO");
	    if(dataxFlowTemplateVO!=null){
	    	request.setAttribute("flowTemplateName", dataxFlowTemplateVO.getFlowTemplateName());
	    	request.setAttribute("flowTemplateId", dataxFlowTemplateVO.getFlowTemplateId());
	    	request.setAttribute("businessType", dataxFlowTemplateVO.getBusinessType());
	    	request.setAttribute("creatCorp", dataxFlowTemplateVO.getCreatCorp());
	    	request.setAttribute("businessType", dataxFlowTemplateVO.getBusinessType());
	    	request.setAttribute("notes", dataxFlowTemplateVO.getNotes());
	    }
		return getJspPath(JSP_PATH_BUSINESS_FLOW_TEMPLATE_ADD);
	}
	
	@SuppressWarnings(value="unchecked")
	@RequestMapping("/addFlowActivityTemplate")  
    public String addFlowActivityTemplate(final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addFlowActivityTemplate");
		
		String activityNo = null;
		activityNo = (String)getHttpSession(request).getAttribute("activityNo");
				
		if (StringUtils.isBlank(activityNo)){
			activityNo = Constants.PREFIX_ACTIVITY_ID + StringUtil.leftPad("1","0",2);
		}else{
			activityNo = Constants.PREFIX_ACTIVITY_ID + StringUtil.leftPad(String.valueOf(Integer.parseInt(activityNo.substring(1))+1),"0",2);
		}
        LOG.debug("activityNo="+activityNo);
        getHttpSession(request).setAttribute("activityNo", activityNo);
        
        List<DataxFlowActorsTemplateVO> dataxFlowActorsTemplateVOList = 
				(List<DataxFlowActorsTemplateVO>)getHttpSession(request).getAttribute("dataxFlowActorsTemplateVOList");
        final Map<String, String> optionNodeMap = new LinkedHashMap<String, String>();
        if (null != dataxFlowActorsTemplateVOList){
        	for(DataxFlowActorsTemplateVO dataxFlowActorsTemplateVO:dataxFlowActorsTemplateVOList){
            	optionNodeMap.put(dataxFlowActorsTemplateVO.getId().getActorNo(),dataxFlowActorsTemplateVO.getActorName());
            }
        }        
        getHttpSession(request).setAttribute("optionNodeMap", optionNodeMap);
        
        Integer activityOrder = (Integer)getHttpSession(request).getAttribute("activityOrder");
        if(null == activityOrder){
        	activityOrder = 1;
        }
        getHttpSession(request).setAttribute("activityOrder", activityOrder);
        
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTIVITY_TEMPLATE_ADD);
	}	
	
	@SuppressWarnings(value="unchecked")
	@RequestMapping("/addFlowActivityTemplateSave")  
    public String addFlowActivityTemplateSave(@RequestParam(value="activityNo",required=false) String activityNo,
			   @RequestParam(value="activityName",required=false) String activityName,
			   @RequestParam(value="activityOrder",required=false) String activityOrder,
			   @RequestParam(value="actorStart",required=false) String actorStart,
			   @RequestParam(value="actorEnd",required=false) String actorEnd,
			   @RequestParam(value="prefixActivityNo",required=false) String prefixActivityNo,
			   @ModelAttribute("DataxFlowActivityTemplateVO") DataxFlowActivityTemplateVO dataxFlowActivityTemplateVO,
			   @ModelAttribute("DataxFlowActivityTemplateIdVO") DataxFlowActivityTemplateIdVO dataxFlowActivityTemplateIdVO,
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addFlowActivityTemplateSave");
		
		List<DataxFlowActivityTemplateVO> dataxFlowActivityTemplateVOList = 
				(List<DataxFlowActivityTemplateVO>)getHttpSession(request).getAttribute("dataxFlowActivityTemplateVOList");
		if(null == dataxFlowActivityTemplateVOList){
			dataxFlowActivityTemplateVOList = new ArrayList<DataxFlowActivityTemplateVO>();
		}
		
		if (!StringUtils.isBlank(dataxFlowActivityTemplateVO.getPrefixActivityNo())) {
			boolean flag = false;
			for (DataxFlowActivityTemplateVO vo : dataxFlowActivityTemplateVOList) {
				if (vo.getId().getActivityNo().equals(dataxFlowActivityTemplateVO.getPrefixActivityNo())) {
					flag = true;
					break;
				}
			}
			if (flag == false) {
				final List<JFValidationBean> errorList = getErrorList();
				errorList.add(ErrorUtil.generateJFValidationFromAppException(new SimpleApplicationException("message.businessflow.perfixActivityNo")));
				request.setAttribute("activityName", activityName);
				request.setAttribute("activityOrder", activityOrder);
				request.setAttribute("actorStart", actorStart);
				request.setAttribute("actorEnd", actorEnd);
				request.setAttribute("prefixActivityNo", prefixActivityNo);
				return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTIVITY_TEMPLATE_ADD);
			}
		}

		dataxFlowActivityTemplateVO.setId(dataxFlowActivityTemplateIdVO);
		dataxFlowActivityTemplateVOList.add(dataxFlowActivityTemplateVO);
		getHttpSession(request).setAttribute("dataxFlowActivityTemplateVOList", dataxFlowActivityTemplateVOList);
		getHttpSession(request).setAttribute("activityOrder", Integer.parseInt(activityOrder)+1);
		
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTIVITY_TEMPLATE);
	}
	
	@SuppressWarnings(value="unchecked")
	@RequestMapping("/removeFlowActivityTemplate")  
    public String removeFlowActivityTemplate(@RequestParam(value="activityNo",required=false) String activityNo,
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.removeFlowActivityTemplate");
		
		List<DataxFlowActivityTemplateVO> dataxFlowActivityTemplateVOList = 
				(List<DataxFlowActivityTemplateVO>)getHttpSession(request).getAttribute("dataxFlowActivityTemplateVOList");
		Integer activityOrder = 0;
		for(DataxFlowActivityTemplateVO dataxFlowActivityTemplateVO:dataxFlowActivityTemplateVOList){
			if(null != activityNo && activityNo.equals(dataxFlowActivityTemplateVO.getId().getActivityNo())){
				dataxFlowActivityTemplateVOList.remove(dataxFlowActivityTemplateVO);
				activityOrder = dataxFlowActivityTemplateVO.getActivityOrder();
				break;
			}
		}
		for(DataxFlowActivityTemplateVO dataxFlowActivityTemplateVO:dataxFlowActivityTemplateVOList){
			if(dataxFlowActivityTemplateVO.getActivityOrder()>activityOrder){
				dataxFlowActivityTemplateVO.setActivityOrder(dataxFlowActivityTemplateVO.getActivityOrder()-1);
			}
		}
		getHttpSession(request).setAttribute("dataxFlowActivityTemplateVOList", dataxFlowActivityTemplateVOList);
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTIVITY_TEMPLATE);
	}
	
	
	@RequestMapping("/addFlowActivityTemplatePrev")  
    public String addFlowActivityTemplatePrev(final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addFlowActivityTemplatePrev");		
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTORS_TEMPLATE);
	}
	
	@SuppressWarnings(value="unchecked")
	@RequestMapping("/addSubmit")  
    public String addSubmit(@RequestParam(value="templateName",required=false) String templateName,
			   @RequestParam(value="templateId",required=false) String templateId,
			   @RequestParam(value="businessType",required=false) String businessType,
			   @RequestParam(value="creater",required=false) String creater,
			   @RequestParam(value="status",required=false) String status,
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.addSubmit");
		final DataxFlowTemplateVO dataxFlowTemplateVO = (DataxFlowTemplateVO)getHttpSession(request).getAttribute("dataxFlowTemplateVO");
		
		final List<JFValidationBean> errorList = getErrorList();
		
		final List<DataxFlowActivityTemplateVO> dataxFlowActivityTemplateVOList = (List<DataxFlowActivityTemplateVO>)getHttpSession(request).getAttribute("dataxFlowActivityTemplateVOList");		
		if (null==dataxFlowActivityTemplateVOList || dataxFlowActivityTemplateVOList.size()<1){
			errorList.add(ErrorUtil.generateJFValidationFromAppException(new SimpleApplicationException("message.businessflow.ActivitySubmit")));
		}		
		
		final List<DataxFlowActorsTemplateVO> dataxFlowActorsTemplateVOList = (List<DataxFlowActorsTemplateVO>)getHttpSession(request).getAttribute("dataxFlowActorsTemplateVOList");
		if (null==dataxFlowActorsTemplateVOList || dataxFlowActorsTemplateVOList.size()<2){
			errorList.add(ErrorUtil.generateJFValidationFromAppException(new SimpleApplicationException("message.businessflow.ActorsSubmit")));
		}

		if(errorList.isEmpty()){
			//isEdit
			String  strIsEdit=(String)getHttpSession(request).getAttribute(IS_EDIT);
			if(!Constants.STATE_YES.equals(strIsEdit)){	
				dataxFlowTemplateVO.setCreater(getCurrentDataxUserBean(request).getDataxUserInfoVO().getUsername());
			}
			try{
				this.dataxFlowTemplateBO.submit(strIsEdit,dataxFlowTemplateVO, dataxFlowActivityTemplateVOList, dataxFlowActorsTemplateVOList);
			}catch(SimpleApplicationException sae){
				errorList.add(ErrorUtil.generateJFValidationFromAppException(sae));
			}
		
		}
		
		
		if (errorList.isEmpty()){
			for(DataxFlowActorsTemplateVO dataxFlowActorsTemplateVO:dataxFlowActorsTemplateVOList){
				for (DataxFlowActivityTemplateVO dataxFlowActivityTemplateVO:dataxFlowActivityTemplateVOList){
					if (!dataxFlowActorsTemplateVO.getId().getActorNo().equals(dataxFlowActivityTemplateVO.getActorEnd())
							&& !dataxFlowActorsTemplateVO.getId().getActorNo().equals(dataxFlowActivityTemplateVO.getActorStart())){
						errorList.add(ErrorUtil.generateJFValidationFromAppException(new SimpleApplicationException("message.businessflow.ActorUnuse")));
						break;
					}
				}	
				if(!errorList.isEmpty()){
					break;
				}
			}
		}
		
		final String returnPage;
		if (errorList.isEmpty()){	
			returnPage = init(1, null, null, null, null, null, null, null, request, response);
		}else {
			request.getSession().removeAttribute(IS_EDIT);
			returnPage = getJspPath(JSP_PATH_BUSINESS_FLOW_ACTIVITY_TEMPLATE);
		}		
		
		return returnPage;
	}
	
	@RequestMapping("/delFlowTemplate")  
    public String delFlowTemplate(@RequestParam(value="flowTemplateId",required=false) String flowTemplateId,
			   final HttpServletRequest request, final HttpServletResponse response)  {
		LOG.debug("Into BusinessFlowController.delFlowTemplate");
		dataxFlowTemplateBO.del(flowTemplateId);
		return init(1, null, null, null, null, null, null, null, request, response);
	}
	
	@RequestMapping("/uptBusinessFlow") 
	public String uptBusinessFlow(@RequestParam(value="flowTemplateId",required=false) String flowTemplateId,
			   final HttpServletRequest request, final HttpServletResponse response){
		DataxFlowTemplateVO dataxFlowTemplateVO = dataxFlowTemplateBO.findById(flowTemplateId);
		final List<JFValidationBean> errorList = getErrorList();
		if (!Constants.TEMPALTE_STATE_TEMP.equals(dataxFlowTemplateVO.getStatus().toString()) ) {
			errorList.add(new JFValidationBean(
					"message.businessflow.Edit"));
		}
		
		String strPath="";
		if(errorList.isEmpty()){
			getHttpSession(request).setAttribute("flowTemplateId", dataxFlowTemplateVO.getFlowTemplateId());
			getHttpSession(request).setAttribute("flowTemplateName", dataxFlowTemplateVO.getFlowTemplateName());
			getHttpSession(request).setAttribute("creatCorp", dataxFlowTemplateVO.getCreatCorp());
			getHttpSession(request).setAttribute("notes", dataxFlowTemplateVO.getNotes());
			getHttpSession(request).setAttribute("businessType", dataxFlowTemplateVO.getBusinessType());
			List<DataxFlowActorsTemplateVO> dataxFlowActorsTemplateVOList = dataxFlowActorsTemplateBO.findByFlowTempId(flowTemplateId);
			List<DataxFlowActivityTemplateVO> dataxFlowActivityTemplateVOList = dataxFlowActivityTemplateBO.findByFlowTempId(flowTemplateId);
			
			int actorNoTemp = 0;
			for(DataxFlowActorsTemplateVO dataxFlowActorsTemplateVO :dataxFlowActorsTemplateVOList){				
				if(actorNoTemp<Integer.parseInt(dataxFlowActorsTemplateVO.getId().getActorNo().substring(1))){
					actorNoTemp = Integer.parseInt(dataxFlowActorsTemplateVO.getId().getActorNo().substring(1));
				}
			}
			String actorNo = Constants.PREFIX_NODE_ID + StringUtil.leftPad(String.valueOf(actorNoTemp),"0",2);
			int activityNoTemp = 0;
			for(DataxFlowActivityTemplateVO dataxFlowActivityTemplateVO:dataxFlowActivityTemplateVOList){				
				if(activityNoTemp<Integer.parseInt(dataxFlowActivityTemplateVO.getId().getActivityNo().substring(1))){
					activityNoTemp = Integer.parseInt(dataxFlowActivityTemplateVO.getId().getActivityNo().substring(1));
				}
			}
			String activityNo = Constants.PREFIX_ACTIVITY_ID + StringUtil.leftPad(String.valueOf(activityNoTemp),"0",2);
			
			getHttpSession(request).setAttribute("actorNo", actorNo);
			getHttpSession(request).setAttribute("activityNo", activityNo);
			getHttpSession(request).setAttribute("dataxFlowActorsTemplateVOList", dataxFlowActorsTemplateVOList);
			getHttpSession(request).setAttribute("dataxFlowActivityTemplateVOList", dataxFlowActivityTemplateVOList);
			getHttpSession(request).setAttribute(IS_EDIT, "Y");
		
			strPath=getJspPath(JSP_PATH_BUSINESS_FLOW_TEMPLATE_ADD);
		}else{
			strPath=init(1, null, null, null, null, null, null, null, request, response);
		}
		
		return strPath;
	}
	
	@RequestMapping("/viewBusinessFlow") 
	public String viewBusinessFlow(@RequestParam(value="flowTemplateId",required=false) String flowTemplateId,
			   final HttpServletRequest request, final HttpServletResponse response){
		DataxFlowTemplateVO dataxFlowTemplateVO = dataxFlowTemplateBO.findById(flowTemplateId);
		getHttpSession(request).setAttribute("flowTemplateId", dataxFlowTemplateVO.getFlowTemplateId());
		getHttpSession(request).setAttribute("flowTemplateName", dataxFlowTemplateVO.getFlowTemplateName());
		getHttpSession(request).setAttribute("creatCorp", dataxFlowTemplateVO.getCreatCorp());
		getHttpSession(request).setAttribute("notes", dataxFlowTemplateVO.getNotes());
		getHttpSession(request).setAttribute("businessType", dataxFlowTemplateVO.getBusinessType());
		List<DataxFlowActorsTemplateVO> dataxFlowActorsTemplateVOList = dataxFlowActorsTemplateBO.findByFlowTempId(flowTemplateId);
		List<DataxFlowActivityTemplateVO> dataxFlowActivityTemplateVOList = dataxFlowActivityTemplateBO.findByFlowTempId(flowTemplateId);
		getHttpSession(request).setAttribute("dataxFlowActorsTemplateVOList", dataxFlowActorsTemplateVOList);
		getHttpSession(request).setAttribute("dataxFlowActivityTemplateVOList", dataxFlowActivityTemplateVOList);
		
		return getJspPath(JSP_PATH_BUSINESS_FLOW_TEMPLATE_VIEW);
	}	
	
	@RequestMapping("/goBackToActorsTemplate")
	public String goBackToActorsTemplate(final HttpServletRequest request,
			final HttpServletResponse response) {
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTORS_TEMPLATE);
	}
	
	@RequestMapping("/goBackToActivityTemplate")
	public String goBackToActivityTemplate(final HttpServletRequest request,
			final HttpServletResponse response) {
		return getJspPath(JSP_PATH_BUSINESS_FLOW_ACTIVITY_TEMPLATE);
	}

	
	private HttpSession getHttpSession(final HttpServletRequest request){
		return request.getSession();
	}
	
	private void removeSessionAttribute(final HttpServletRequest request){		
		for (String attrName:ATTR_NAME){
			LOG.debug("remove session attr:"+attrName);
			request.getSession().removeAttribute(attrName);
		}
	}
	
	@Override
    protected String getBaseJspPath() {
        return JSP_PATH;
    }
}